package com.online.service;

import com.online.dao.TeacherMapper;
import com.online.dao.UserMapper;
import com.online.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public Teacher showATeacher(String workNumber) {
        return teacherMapper.selectByWorkNumber(workNumber);
    }

    @Override
    public Integer showStatusByWorkNumber(String workNumber) {
        return teacherMapper.selectStatusByWorkNumber(workNumber);
    }

    @Override
    public Teacher showATeacher(Integer teacherId) {
        return teacherMapper.selectByTeacherId(teacherId);
    }

    @Override
    public int updateTeacher(Integer teacherId, String userName, String password,String email) {
        return teacherMapper.updateTeacherInfo(teacherId,userName,password,email);
    }

    @Override
    public int insertATeacher(String workNumber, String password, String email) {
        return teacherMapper.insertTeacherInfo(workNumber,password,email);
    }

    @Override
    public List<Teacher> showAllTeacherInfo() {
        return teacherMapper.selectAllTeacher();
    }

    @Override
    public int updateTeacherStatus(Integer teacherId, Integer status) {
        return teacherMapper.updateTeacherStatus(teacherId,status);
    }

    @Override
    public Boolean findTeacherByEmail(String email) {

        //根据email查询是否有教师信息
        Teacher teacher = teacherMapper.selectByEmail(email);

        //教师没有注册
        if(teacher == null) {
            return false;
        }else {
            //教师已被注册
            return true;
        }
    }

    @Override
    public Boolean updatePassword(String email, String password) {

        //更新密码，更新成功返回true，失败返回false
        int result = teacherMapper.updatePasswordByEmail(email,password);

        //根据email查询一个老师的详情
        Teacher teacher = teacherMapper.selectByEmail(email);

        //获取老师的工号
        String workNumber = teacher.getWorkNumber();

        //根据工号更新教师的登录信息
        result = userMapper.updateUserByUserNumber(workNumber,password);

        if(result != 0) {
            return true;
        }else {
            return false;
        }
    }
}